//@dart=2.12
import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/abnormal_event_center/abnormal_event_center_model.dart';
import 'package:ls_work/abnormal_event_center/response/abnormal_event_list_data.dart';
import 'package:ls_work/model/building_list_model.dart';
import 'package:ls_work/util/ext_utils.dart';

class AbnormalEventCenterViewModel
    extends BaseListViewModel<AbnormalEventListData> {
  AbnormalEventCenterViewModel(BuildContext context) : super(context);

  Map? topMap;
  var model = AbnormalEventCenterModel();

  String? elderNameLike;

  String? bedNoLike;

  var buildingModel = BuildingListModel();

  List<PickerDataWithIdModel> buildingList = [];

  PickerDataWithIdModel? building;

  PickerDataWithIdModel? eventClass;

  PickerDataWithIdModel? status;

  @override
  Future<List<AbnormalEventListData>> requestListData() async {
    if (buildingList.isEmpty) {
      var list = await buildingModel.getBuildingList();
      buildingList = list
          .map((e) => PickerDataWithIdModel(
              pickId: e.id.toString(), pickName: e.buildingNo))
          .toList();
    }
    topMap = await model.abnormalStatistics();
    return await model.abnormalEventList(
        page: page,
        rows: pageLength,
        elderNameLike: elderNameLike,
        bedNoLike: bedNoLike,
        buildingId: building?.pickId,eventClass: eventClass?.pickId,status: status?.pickId);
  }

  void chooseEventClass(){
    var items = [
      PickerDataWithIdModel(pickId: '300',pickName: '体征异常'),
      PickerDataWithIdModel(pickId: '400',pickName: '3天未大便'),
      PickerDataWithIdModel(pickId: '500',pickName: '30天体重未量'),
      PickerDataWithIdModel(pickId: '600',pickName: '60天体重未量'),
      PickerDataWithIdModel(pickId: '700',pickName: '60天体重未量'),
    ];
    DataPickerView(context, items,currentFirstData: eventClass,onSubmitListener: (p1,p2,p3){
      eventClass = items[p1];
      notifyListeners();
      requestRefresh();
    }).show();
  }

  void chooseStatus(){
    var items = [
      PickerDataWithIdModel(pickId: '0',pickName: '预警中'),
      PickerDataWithIdModel(pickId: '1',pickName: '已关闭'),
    ];
    DataPickerView(context, items,currentFirstData: status,onSubmitListener: (p1,p2,p3){
      status = items[p1];
      notifyListeners();
      requestRefresh();
    }).show();
  }

  void chooseBuilding() {
    DataPickerView(context, buildingList, currentFirstData: building,
        onSubmitListener: (p1, p2, p3) {
      building = buildingList[p1];
      notifyListeners();
      requestRefresh();
    }).show();
  }

  void resetClick(){
    elderNameLike = null;
    bedNoLike = null;
    building = null;
    eventClass = null;
    status = null;
    notifyListeners();
    requestRefresh();
  }

  @override
  void onCreated() {
    super.onCreated();
    requestRefresh();
  }

  void closeEvent(dynamic id) {
    showApplyDialog(context: context, onSubmit: () async {
      showLoadingDialog(msg: '提交中...');
      try{
        await model.closeEvent(id);
        hideDialog();
        requestRefresh();
      }catch(e){
        hideDialog();
        showToast(e.toNetError());
      }
    },message: '确定要关闭吗？');
  }
}
